const {
  toUrl,
  globalData
} = getApp();
import {
  request
} from "@/utils/request";

Page({
  toUrl,
  data: {
    globalData,
    tabAvtive: false,
    wcCarNum: false,
    CarNum: null
  },
  handleCarNum() {
    wx.chooseLicensePlate({
      success: (e) => {
        console.log(e)
        if (e.errMsg == 'chooseLicensePlate:ok') {
          // this.ksCar(e.plateNumber)
          this.setData({
            tabAvtive: true,
            wcCarNum: true,
            CarNum: e.plateNumber
          })
          this.wxCarNumHandle()
        }
        // this.setData({
        //     cphm:e.plateNumber
        // })
      }
    })
  },
  wxCarNumHandle() {
    let data = {
      plateNo: this.data.CarNum,
    };
    request("Userscar/add", {
      data
    }).then((res) => {
      if (res.errcode == 1) {
        wx.showToast({
          title: "添加成功",
        });
        let value = []
        let value1 = []
        let copyCarNum = this.data.CarNum.toString().split('')
        console.log(copyCarNum.length);
        copyCarNum.forEach((it, idx) => {
          if (idx < 2) {
            value[idx] = it
          } else {
            value1[idx - 2] = it
          }
        })
        const values = this.selectComponent("#carAlert")
        values.setData({
          value1,
          value,
        })

        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 2000);

      } else {
        wx.showToast({
          title: res.message,
        });
      }

    });
  },
  next() {

    const value = this.selectComponent("#carAlert").data.value;
    const value1 = this.selectComponent("#carAlert").data.value1;
    // console.log(value,value1);
    let len = value.join('') + value1.join('')
    console.log(len);
    if (value) {
      console.log(len.length);
      if (len.length < 7) {
        wx.showToast({
          title: '车牌不符合规则',
          icon: 'error'
        })
        return
      }
    }
    let data = {
      plateNo: len,
    };
    request("Userscar/add", {
      data
    }).then((res) => {
      if (res.errcode == 1) {
        wx.showToast({
          title: "添加成功",
        });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 2000);

      } else {
        wx.showToast({
          title: res.message,
        });
      }
      const values = this.selectComponent("#carAlert")
      values.setData({
        value: '',
        value1: []
      })
    });
  },
  onLoad(options) {
    const carAlert = this.selectComponent("#carAlert");
    carAlert.setData({
      value1: [],
      value: [],
    })
  },
  conmitTag(data) {
    console.log(data.detail);
    this.setData({
      tabAvtive: data.detail
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});